package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import model.Periferico;

public class PerifericoDAO extends PersistenciaDAO {
	
	
	public Collection<HashMap<String, Object>> buscarDadosPerifericoBrowse() throws SQLException {

		Collection<HashMap<String, Object>> result = new ArrayList<HashMap<String,Object>>();
		Statement sta = connection.createStatement();
		ResultSet elements = sta.executeQuery(
				" SELECT p.cd_periferico as codigo, "+
				"  		p.ds_periferico as descricao "+
				" FROM quiosque.periferico p "+
				" ORDER BY p.cd_periferico ");
		
		while (elements.next()) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("codigo", elements.getInt("codigo"));
			map.put("descricao", elements.getString("descricao"));
			result.add(map);
		}
		return result;
	}

	public Periferico buscarPerifericoPorId(int id) throws SQLException {
		Periferico result = null;
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM quiosque.periferico WHERE cd_periferico = " + id);

		while (elements.next()) {
			result = montarPeriferico(elements);
		}
		return result;
	}

	public Periferico buscarPerifericoPorDescricao(String descricao) throws SQLException {
		Periferico result = null;
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM quiosque.periferico WHERE ds_periferico = '" + descricao +"'");

		while (elements.next()) {
			result = montarPeriferico(elements);
		}
		return result;
	}

	public Periferico montarPeriferico(ResultSet elements) throws SQLException {
		Periferico periferico = new Periferico();
		periferico.setId(elements.getInt("cd_periferico"));
		periferico.setDescricao(elements.getString("ds_periferico"));
		return periferico;
	}

	public Collection<Periferico> buscarPerifericoPorComputador(int idComputador) throws SQLException{
		Collection<Periferico> perifericos = new ArrayList<Periferico>();

		Statement sta = connection.createStatement();
		ResultSet elements = sta
		.executeQuery("SELECT * FROM computadorperiferico WHERE cd_computador = "	+ idComputador);

		while (elements.next()) {
			Periferico periferico = buscarPerifericoPorId(elements.getInt("cd_periferico"));
			perifericos.add(periferico);
		}

		return perifericos;
	}
	
	public Collection<Periferico> buscarTodos() throws SQLException {
		Collection<Periferico> result = new ArrayList<Periferico>();
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM quiosque.periferico ");

		while (elements.next()) {
			Periferico periferico = montarPeriferico(elements);
			result.add(periferico);
		}
		return result;
	}

}
